package middleware

import (
	"net/http"
	"pt-gin-mongo/error"
	"pt-gin-mongo/utils"

	"github.com/gin-gonic/gin"
)

func ResponseFormat() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				if apiError, ok := err.(*error.ApiError); ok {
					utils.ResponseApiError(c, *apiError)
					return
				}
				// 如果有错，打印日志
				c.AbortWithStatus(http.StatusInternalServerError)
			}
		}()

		c.Next()
	}
}
